package com.weds.edwx.service;

import com.alibaba.fastjson.JSONObject;
import com.weds.core.utils.coder.Coder;
import com.weds.edwx.entity.SbOpenIdReq;
import com.weds.edwx.util.SbUtils;
import com.weds.edwx.util.TestApproval;
import com.weds.pay.lcsw.constants.LcswConstants;
import com.weds.pay.lcsw.constants.LcswParams;
import com.weds.pay.lcsw.entity.*;
import com.weds.pay.lcsw.utils.LcswUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.util.List;

@Component
public class SbService {
    @Resource
    private LcswParams lcswParams;

    @Resource
    private RestTemplate restTemplate;

/*    public String getOpenId(){
        SbOpenIdReq sbOpenIdReq=new SbOpenIdReq();
        sbOpenIdReq.setMerchantNo(lcswParams.getMerchantNo());
        sbOpenIdReq.setRedirectUri(lcswParams.getRedirecOpenIdUrl());
        sbOpenIdReq.setTerminalNo(lcswParams.getTerminalId());
        JSONObject temp1 = JSONObject.parseObject(JSONObject.toJSONString(sbOpenIdReq));

        System.out.println("temp1111="+temp1);
        StringBuilder sb1 = new StringBuilder();
        List<String> list1 = SbUtils.sign(SbOpenIdReq.class);
        for (String key : list1) {
             System.out.println("key="+key);
            sb1.append(key).append("=").append(temp1.getString(key)).append("&");
        }
        sb1.append("access_token").append("=").append(lcswParams.getAccessToken());
        System.out.println("sb111"+sb1.toString());
        sbOpenIdReq.setKeySign(Coder.md5Hex(sb1.toString()));

        System.out.println("md5Hexsb1="+Coder.md5Hex(sb1.toString()));
        //HttpHeaders headers = new HttpHeaders();
        //headers.setContentType(MediaType.APPLICATION_JSON);
        //HttpEntity<String> request = new HttpEntity<>(JSONObject.toJSONString(sbOpenIdReq), headers);
        //System.out.println("请求参数：：："+request);
        String openIdUrl=lcswParams.getOpenIdUrl();
        String resp="";
        try{
            openIdUrl+="?merchant_no="+lcswParams.getMerchantNo()+"&terminal_no="+lcswParams.getTerminalId()+"&redirect_uri="+URLEncoder.encode(lcswParams.getRedirecOpenIdUrl(),"UTF-8")+"&key_sign="+Coder.md5Hex(sb1.toString())+"";
            System.out.println("请求链接="+openIdUrl);
            String sr = TestApproval.sendGet(lcswParams.getOpenIdUrl(),"merchant_no="+lcswParams.getMerchantNo()+"&terminal_no="+lcswParams.getTerminalId()+"&redirect_uri="+URLEncoder.encode(lcswParams.getRedirecOpenIdUrl(),"UTF-8")+"&key_sign="+Coder.md5Hex(sb1.toString())+"");
            //ResponseEntity<String> res  = restTemplate.getForEntity(openIdUrl,String.class);
            //System.out.println("respppp11="+res.getBody());
            //resp= res.getBody();
            System.out.println("respppppp="+sr);
            return resp;
        } catch (Exception e) {
            e.printStackTrace();
            return resp;
        }
    }*/


    public PayPreRstResp wxPrePay(PayPreReq payPreReq) throws Exception {
        payPreReq.setPayVer("100");
        payPreReq.setPayType("010");
        payPreReq.setServiceId("012");
        payPreReq.setMerchantNo(lcswParams.getMerchantNo());
        payPreReq.setTerminalId(lcswParams.getTerminalId());

        JSONObject temp1 = JSONObject.parseObject(JSONObject.toJSONString(payPreReq));
        //System.out.println("temp1="+temp1);
        StringBuilder sb1 = new StringBuilder();
        List<String> list1 = SbUtils.sign(PayPreReq.class);
        //System.out.println("list1="+list1);
        for (String key : list1) {
           // System.out.println("key="+key);
            sb1.append(key).append("=").append(temp1.getString(key)).append("&");
        }
        //System.out.println("sb1="+sb1.toString());
        // for (String key : temp1.keySet()) {
        //     sb1.append(key).append("=").append(temp1.getString(key)).append("&");
        // }
        sb1.append("access_token").append("=").append(lcswParams.getAccessToken());
        //System.out.println("sb2="+sb1.toString());
        payPreReq.setKeySign(Coder.md5Hex(sb1.toString()));

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(JSONObject.toJSONString(payPreReq), headers);
        //System.out.println("请求参数：：："+request);
        String resp = restTemplate.postForObject(lcswParams.getWxPrePayUrl(), request, String.class);
        //System.out.println("resppp=="+resp);
        LcswResp lcswResp = JSONObject.parseObject(resp, LcswResp.class);
       // System.out.println("lcswResp=="+lcswResp);
        if (lcswResp == null || !LcswConstants.HTTP_SUCCESS.equals(lcswResp.getReturnCode())) {
            return null;
        }

        PayPreResp payPreResp = JSONObject.parseObject(resp, PayPreResp.class);
        //System.out.println("payPreResp=="+payPreResp);
        if (payPreResp == null || !LcswConstants.HTTP_SUCCESS.equals(payPreResp.getResultCode())) {
            return null;
        }
        //System.out.println("payPreResp=toJSONString"+JSONObject.toJSONString(payPreResp));
        JSONObject temp2 = JSONObject.parseObject(JSONObject.toJSONString(payPreResp));
        StringBuilder sb2 = new StringBuilder();

        List<String> list2 = SbUtils.sign(PayPreResp.class);
        for (String key : list2) {
           // System.out.println("key1="+key);
            sb2.append(key).append("=").append(temp2.getString(key));
        }
        //.append("&")
        //sb2.append("access_token").append("=").append(lcswParams.getAccessToken());

        //System.out.println("Coder=="+(sb2.toString()));

        //System.out.println("Coder=="+Coder.md5Hex(sb2.toString()));

/*        if (!lcswResp.getKeySign().equals(Coder.md5Hex(sb2.toString()))) {
            return null;
        }*/

        PayPreRstResp payPreRstResp = JSONObject.parseObject(resp, PayPreRstResp.class);

        //System.out.println("payPreRstResp=="+payPreRstResp);

        payPreRstResp.setPayPreResp(payPreResp);
        return payPreRstResp;
    }


    public NoticeReq payNotice(NoticeReq noticeReq) {

       //System.out.println("NoticeReq=toJSONString"+JSONObject.toJSONString(noticeReq));

       JSONObject temp = JSONObject.parseObject(JSONObject.toJSONString(noticeReq));

        StringBuilder sb = new StringBuilder();
        List<String> list = SbUtils.sign(NoticeReq.class);
        for (String key : list) {
            sb.append(key).append("=").append(temp.getString(key)).append("&");
        }
        sb.append("access_token").append("=").append(lcswParams.getAccessToken());

 /*       if (!noticeReq.getKeySign().equals(Coder.md5Hex(sb.toString()))) {
            return null;
        }*/
        return noticeReq;
    }

    public PayQueryRstResp payQuery(PayQueryReq payQueryReq) throws Exception {
        payQueryReq.setPayVer("100");
        payQueryReq.setPayType("010");
        payQueryReq.setServiceId("020");

        payQueryReq.setMerchantNo(lcswParams.getMerchantNo());
        payQueryReq.setTerminalId(lcswParams.getTerminalId());

        JSONObject temp1 = JSONObject.parseObject(JSONObject.toJSONString(payQueryReq));
        System.out.println("temp1="+temp1);
        StringBuilder sb1 = new StringBuilder();
        List<String> list1 = SbUtils.sign(PayQueryReq.class);
        for (String key : list1) {
            System.out.println("key="+key);
            sb1.append(key).append("=").append(temp1.getString(key)).append("&");
        }
        sb1.append("access_token").append("=").append(lcswParams.getAccessToken());
        System.out.println("sb1="+sb1);
        payQueryReq.setKeySign(Coder.md5Hex(sb1.toString()));

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(JSONObject.toJSONString(payQueryReq), headers);
        System.out.println("请求参数：：："+request);
        String resp = restTemplate.postForObject(lcswParams.getPayQueryUrl(), request, String.class);
        System.out.println("ressssp="+resp);
        LcswResp lcswResp = JSONObject.parseObject(resp, LcswResp.class);
        if (lcswResp == null || !LcswConstants.HTTP_SUCCESS.equals(lcswResp.getReturnCode())) {
            return null;
        }

        PayQueryResp payQueryResp = JSONObject.parseObject(resp, PayQueryResp.class);
        if (payQueryResp == null || !LcswConstants.HTTP_SUCCESS.equals(payQueryResp.getResultCode())) {
            return null;
        }

        JSONObject temp2 = JSONObject.parseObject(JSONObject.toJSONString(payQueryResp));
        StringBuilder sb2 = new StringBuilder();
        List<String> list2 = SbUtils.sign(PayQueryResp.class);
        for (String key : list2) {
            sb2.append(key).append("=").append(temp2.getString(key)).append("&");
        }

        // for (String key : temp2.keySet()) {
        //     sb2.append(key).append("=").append(temp2.getString(key)).append("&");
        // }
        sb2.append("access_token").append("=").append(lcswParams.getAccessToken());
/*        if (!lcswResp.getKeySign().equals(Coder.md5Hex(sb2.toString()))) {
            return null;
        }*/

        PayQueryRstResp payQueryRstResp = JSONObject.parseObject(resp, PayQueryRstResp.class);
        payQueryRstResp.setPayQueryResp(payQueryResp);
        return payQueryRstResp;
    }
}
